<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>

<form>
    账户名称： <select name="outName" id="outName">
                    <option value="">--选择转出账户--</option>
              </select> <br>
    账户余额： <input type="text" name="balance" id="balance"> <br>
    收款账户： <input type="text" name="inName" id="inName"> <br>
    转账金额： <input type="text" name="money" id="money"> <br>

    <input type="button" value="转账" id="transfer">
</form>

<script>
    //查询当前登录用户的所有账户
    $.get("/account/user",{},function (r){
        if (r.code == 1000) {
            let accounts = r.data;

            $("#outName").html("<option value=\"\">--选择转出账户--</option>")

            $.each(accounts,function (idx,account){
                $("#outName").append("<option value='"+account.id+"'>"+account.name+"</option>")
            })

        }else{
            alert(r.msg)
        }
    },"json")

    //修改账户时，获取账户余额
    $("#outName").change(function (){
        let id = this.value;
        //alert(this.value)
        $.get("/account/"+id,{},function (r){
            if (r.code == 1000){
                let account = r.data;
                //设置余额
                $("#balance").val(account.money)
            }else{
                alert(r.msg)
            }
        },"json")
    })

    $("#transfer").click(function (){
        //获取转出账户
        let outId = $("#outName").val();
        //获取转入账户
        let inName = $("#inName").val();
        //获取转账金额
        let money = $("#money").val();

        let params = {
            outId:outId,
            inName:inName,
            money:money
        }

        $.post("/account/transfer",params,function (r){
            if (r.code == 1000){
                location.href = "index.html"
            }else{
                alert(r.msg)
            }
        },"json");
    })
</script>
</body>
</html>